package praktikumid.p10_e;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

public class Kimp extends JFrame {
	
	class Circle {
		public Point leftUpper;
		public Point start;
		public Point end;
		public int radius;
		
		public Color color = Color.BLACK;
		
		
		private void checkPoints() {
			leftUpper = new Point();
			int minX = Math.min(start.x, end.x);
			int maxX = Math.max(start.x, end.x);
			int minY = Math.min(start.y, end.y);
			int maxY = Math.max(start.y, end.y);
			leftUpper.x = minX;
			leftUpper.y = minY;
			end.x = maxX;
			end.y = maxY;
			/*
			System.out.println(start + "" + end);
			System.out.println(minX + "" + minY);
			System.out.println(maxX + "" + maxY);
			*/
		}
		public void setStart(Point start) {
			this.leftUpper = start;
			this.start = start;
			this.end = start;
		}
		
		
		public void setEnd(Point end) {
			this.end = end;
			checkPoints();
		}
		
		public int getWidth() {
			return end.x - leftUpper.x;
		}
		
		public int getHeight() {
			return end.y - leftUpper.y;
		}
		
		public void resize(int x, int y) {
			end.x += x;
			end.y += y;
			checkPoints();
		}
	}

	
	public List<Circle> circles = new ArrayList<Circle>();
	
	public boolean resizeMode = false;
	
	public Circle activeCircle;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Kimp k = new Kimp("Kimp 0.1", 800, 600);
	}
	
	public Kimp(String title, int width, int height) {
		this.setTitle(title);
		this.setSize(width, height);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mousePressed(e);
				Kimp k = (Kimp)e.getComponent();
				if (!k.resizeMode) {
					Circle c = new Circle();
					c.setStart(e.getPoint());
					k.circles.add(c);
					((Kimp)e.getComponent()).repaint();
				}
			}
		});
		
		this.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseDragged(e);
				
				Kimp k = (Kimp)e.getComponent();
				if (k.resizeMode) {
					// resize
					if (k.activeCircle != null) {
						k.activeCircle.setEnd(e.getPoint());
					}
					
				} else {
					Circle last = k.circles.get(k.circles.size() - 1);
					last.setEnd(e.getPoint());
				}
				k.repaint();
				
			}
			
			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseMoved(e);
				Kimp k = (Kimp)e.getComponent();
				if (k.resizeMode == true && k.circles.size() > 0) {
					Circle found = null;
					for (Circle c : k.circles) {
						double dist = c.leftUpper.distanceSq(e.getPoint().x, e.getPoint().y);
						if (dist < 2000) {
							found = c;
							break;
						}
					}
					if (found != null) {
						for (Circle c : k.circles) {
							c.color = Color.BLACK;
						}
						found.color = Color.RED;
						k.repaint();
						k.activeCircle = found;
					} else {
						k.activeCircle = null;
					}
				}
			}
		});
		
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyPressed(e);
				
				if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
					Kimp k = (Kimp)e.getComponent();
					k.resizeMode = true;
				}
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				super.keyReleased(e);
				if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
					Kimp k = (Kimp)e.getComponent();
					k.resizeMode = false;
					for (Circle c : k.circles) {
						c.color = Color.BLACK;
					}
					k.repaint();
				}
			}
		});
		
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		
		for (Circle c : circles) {
			g.setColor(c.color);
			g.drawOval(c.leftUpper.x, c.leftUpper.y, c.getWidth(), c.getHeight());
		}
	}

}
